package com.xixibaby.pay;

import com.egzosn.pay.ali.bean.AliTransferType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.RefundOrder;
import com.egzosn.pay.common.bean.TransferOrder;
import com.egzosn.pay.wx.bean.WxTransactionType;
import com.egzosn.pay.wx.bean.WxTransferType;
import com.xixibaby.core.SpringUtil;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author tooomj
 * 用于框架支付能力以及系统支付能力之间的适配。
 */
@Component
public class PayAdapt {

    /**
     * app 统一下单
     * @param order
     * @param payType
     * @return
     */
    public Map<String, Object> orderInfo(PayOrder order, PayType payType) {
        Pay pay = SpringUtil.getBean(Pay.class, payType.getCode());
        return pay.appOrder(order);
    }

    /**
     * 回调参数验证签名
     * @param map
     * @param payType
     * @return
     */
    public boolean verifyCallback(Map<String, Object> map,PayType payType){
        Pay pay = SpringUtil.getBean(Pay.class, payType.getCode());
        return pay.verify(map);
    }

    /**
     * 回调参数验证签名
     * @return
     */
    public Map<String, Object> getParameter2Map(Map<String, String[]> parameterMap, InputStream is,PayType payType){
        Pay pay = SpringUtil.getBean(Pay.class, payType.getCode());
        return pay.getParameter2Map(parameterMap,is);
    }

    /**
     * 交易查询
     * @param tradeNo
     * @param outTradeNo
     * @param payType
     * @return
     */
    public Map<String, Object> query(String tradeNo, String outTradeNo,PayType payType){
        Pay pay = SpringUtil.getBean(Pay.class, payType.getCode());
        return pay.query(tradeNo,outTradeNo);
    }


    /**
     * 转账
     * @param money 金额
     * @param outNo 单号
     * @param name 真实姓名
     * @param target 微信openid，支付宝登录账号
     * @param payType 类型 支付宝/微信
     * @return
     */
    public Map<String, Object> transfer(BigDecimal money, String outNo, String name, String target, String remark, PayType payType){
        TransferOrder order = new TransferOrder();
        order.setAmount(money);
        order.setOutNo(outNo);
        order.setPayeeAccount(target);
        order.setPayeeName(name);
        order.setRemark(remark);
        if(payType == PayType.WECHAT || payType == PayType.WECHAT_MICRO_PRO){
            order.setTransferType(WxTransferType.TRANSFERS);
        }else{
            order.setTransferType(AliTransferType.ALIPAY_LOGONID);
        }

        Pay pay = SpringUtil.getBean(Pay.class, payType.getCode());
        return pay.transfer(order);
    }


    /**
     * 退款申请
     * @param tradeNo  交易号  自己的退款交易号
     * @param outTradeNo  微信支付宝的需要退款的那边交易号
     * @param totalMoney 单子总共多少钱
     * @param money  本次需要退多少钱
     * @param payType 退款方式  微信/支付宝
     * @return
     */
    public Map<String, Object> refund(String refundNo,String tradeNo, String outTradeNo, BigDecimal totalMoney, BigDecimal money, PayType payType){
        RefundOrder order = new RefundOrder();
        order.setOutTradeNo(outTradeNo);
        order.setTotalAmount(totalMoney);
        order.setRefundAmount(money);
        order.setTradeNo(tradeNo);
        order.setRefundNo(refundNo);

        Pay pay = SpringUtil.getBean(Pay.class, payType.getCode());
        return pay.refund(order);
    }

}
